﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using Alicanto.Helpers;
using Alicanto.Lexing;

namespace Alicanto.Compilation.Code.Operators.String
{
    internal class OpConcat : ICodeGenerator
    {
        public Expression Code { get; private set; }

        public IEnumerable<ParseInstruction> Generate(TokenSequence tokens, Scope scope)
        {
            GetEvaluation evaluation = new GetEvaluation();
            yield return evaluation;

            ParseExpressionRemainder remainder = new ParseExpressionRemainder();
            yield return remainder;

            Expression x = Conversion.EnsureString(evaluation.Expression);
            Expression y = Conversion.EnsureString(remainder.Remainder);

            this.Code = Expression.Call(
                null,
                typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }),
                x,
                y
            );
        }
    }
}
